package com.zsule.service.impl;

import java.util.List;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.zsule.entity.EUserAddress;
import com.zsule.frameworks.mongodb.service.MongoAbstractService;
import com.zsule.service.IUserAddressService;

@Service("euserAddressService")
public class EUserAddressServiceImpl extends MongoAbstractService<EUserAddress> implements IUserAddressService {

	@Override
	public List<EUserAddress> getAddressByUserId(String userId) {
		return findList(Criteria.where("userId").is(userId));
	}

	@Override
	public EUserAddress getEUserAddressById(String id) {
		return findOne(Criteria.where("id").is(id));
	}

	@Override
	public EUserAddress getMyDefaultAddress(String userId) {
		Query query = new Query(Criteria.where("userId").is(userId));
		query.addCriteria(Criteria.where("isDefault").is(true));
		return findOne(query);
	}

	@Override
	public void setDefault(String id, String userId) {
		Update update = new Update();
		update.set("isDefault", false);
		updateFirst(new Query(Criteria.where("userId").is(userId)), update);

		update.set("isDefault", true);
		updateFirst(new Query(Criteria.where("id").is(id)), update);
	}

}
